/*
 * main.c
 *
 *  Created on: Jul 28, 2013
 *      Author: lxzhu
 */

#include "stdafx.h"
#include <math.h>
void onDraw();
void onMouse(int button, int state, int x, int y);

GLfloat* rotateTransform(GLfloat* v);

GLfloat v[3][2] = { { -0.75, -0.75 }, { 0.75, -0.75 }, { 0, 0.75 } };
int maxLevel = 0;
float factor = 0.5;
int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutCreateWindow("TransformEdgeFlag");
	glutDisplayFunc(onDraw);
	glutMouseFunc(onMouse);
	glutMainLoop();
	return 0;
}

void onDraw() {
	glClear(GL_COLOR_BUFFER_BIT);
	glPolygonMode(GL_FRONT, GL_LINE);
	divideTriangle(v[0], v[1], v[2], maxLevel, 0, rotateTransform);
	glutSwapBuffers();
}
GLfloat* rotateTransform(GLfloat* v) {
	float distance = sqrt(v[0] * v[0] + v[1] * v[1]);
	float theta = factor * distance;
	GLfloat* ret = (GLfloat*) malloc(2 * sizeof(GLfloat));
	ret[0] = cos(theta) * v[0] - sin(theta) * v[1];
	ret[1] = cos(theta) * v[0] + sin(theta) * v[1];
	return ret;
}
void onMouse(int button, int state, int x, int y) {
	int modifier = glutGetModifiers();

	if (GLUT_ACTIVE_CTRL & modifier) {
		if (button == GLUT_RIGHT_BUTTON) {
			factor += 0.1;
		} else {
			if (factor > 0.1)
				factor -= 0.1;
		}
	} else {
		if (button == GLUT_RIGHT_BUTTON) {
			if(maxLevel<5){
				maxLevel++;
			}
		} else {
			if (maxLevel > 0)
				maxLevel--;
		}
	}
    glutPostRedisplay();
}
